package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravlesService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVOService;
import cn.wolfcode.luowowo.cache.service.IUserRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import cn.wolfcode.luowowo.cache.vo.UserFavorsVO;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcoe.luowowo.common.util.MyBeanUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 监听器初始化StrategyStatisVO数据
 */
//@Component
public class RedisDataInitListener implements ApplicationListener <ContextRefreshedEvent> {
    @Reference
    private IStrategyStatisVOService strategyStatisVOService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelStatisVOService travlesStatisVOService;
    @Reference
    private ITravlesService travlesService;
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private IUserRedisService userRedisService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        //初始化攻略相关的数据
        //获取所有的攻略
        List <StrategyDetail> list = strategyDetailService.list();
        //创建vo对象
        StrategyStatisVO vo;
        for (StrategyDetail detail : list) {
            String key = RedisKeys.STRATEGY_STATIS_VO.join( detail.getId().toString() );
            if (strategyStatisVOService.checkStrategyStatisVO( key )) {
                continue;
            }
            vo = new StrategyStatisVO();
            MyBeanUtil.copyProperties( vo, detail );
            vo.setDestId( detail.getDest().getId() );
            vo.setDestName( detail.getDest().getName() );
            vo.setStrategyId( detail.getId() );
            strategyStatisVOService.saveStrategyStatisVO( vo );

        }
        //初始化热门排行
        initStrategyStatisZset( list, RedisKeys.STRATEGY_STATIS_ZSET_HOTTOP );
        initStrategyStatisZset( list, RedisKeys.STRATEGY_STATIS_ZSET_COMMEND );


        //初始化游记相关的数据
        List <Travel> tlist = travlesService.list();
        TravelStatisVO tvo;
        for (Travel t : tlist) {
            String key = RedisKeys.STRATEGY_STATIS_VO.join( t.getId().toString() );
            if (strategyStatisVOService.checkStrategyStatisVO( key )) {
                continue;
            }
            tvo = new TravelStatisVO();
            MyBeanUtil.copyProperties( tvo, t );

            tvo.setTravelId( t.getId() );
            travlesStatisVOService.saveTravelStatisVO( tvo );

        }
        //初始化用户收藏
        List <UserInfo> userInfos = userInfoService.list();
        for (UserInfo u : userInfos) {
            String key = RedisKeys.USER_FAVOS.join( u.getId().toString() );
//            if(strategyStatisVOService.checkStrategyStatisVO( key )){
//                continue;
//            }
            List <Long> tids = userInfoService.selectTravelsIds(u.getId());
            List <Long> sids = userInfoService.selectStrategysIds(u.getId());
            UserFavorsVO userFavorsVO = new UserFavorsVO();
            userFavorsVO.setTravelsIds( tids );
            userFavorsVO.setStrategyIds( sids );
            userRedisService.saveUserFavors(u.getId(),userFavorsVO);
        }


    }

    //初始化攻略数据
    private void initStrategyStatisZset(List <StrategyDetail> list, RedisKeys zsetRey) {
        String setkey = zsetRey.getPrifx();
        int score;
        for (StrategyDetail d : list) {
            String zvalue = RedisKeys.STRATEGY_STATIS_VO.join( d.getId().toString() );

            if (strategyStatisVOService.checkStrategyRankTopValue( setkey, zvalue )) {
                continue;
            }
            if (zsetRey == RedisKeys.STRATEGY_STATIS_VO) {
                score = d.getReplynum() + d.getViewnum();
            } else {
                score = d.getReplynum() + d.getViewnum();
            }
            strategyStatisVOService.initStrategyHotTop( setkey, zvalue, score );
        }
    }
}
